//
//  NumberInputCell.swift
//  Wmsmobile
//
//  Created by Nick xu on 16/8/5.
//  Copyright © 2016年 hddden. All rights reserved.
//

import UIKit
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l < r
  case (nil, _?):
    return true
  default:
    return false
  }
}

fileprivate func > <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
  switch (lhs, rhs) {
  case let (l?, r?):
    return l > r
  default:
    return rhs < lhs
  }
}


protocol HDNumberInputCellDelegate: NSObjectProtocol {
    func finishNumInput(_ cell: HDNumberInputCell)
}

class HDNumberInputCell: HDFounctionBaseCell , UITextFieldDelegate , HDInputViewStateProtocol , HDgetAndSetProtocol{
    //MARK: - property
    weak var delegate:HDNumberInputCellDelegate?
    var inputState:inputViewState = inputViewState.disabled
    var precisionBox: Int?
    var precisionBag: Int?
    var state:inputViewState?{
        get{
            return inputState
        }
        set{
            inputState = newValue!
            switch inputState { //改变状态
            case .activated:
                self.isUserInteractionEnabled = true
                self.boxNumberTextField.becomeFirstResponder()
//                self.boxNumberTextField.layer.borderColor = UIColor.hexStringToUIColor("#0ebfb1").CGColor
//                self.bagNumberTextField.layer.borderColor = UIColor.hexStringToUIColor("#0ebfb1").CGColor
            case .disabled:
                self.isUserInteractionEnabled = false
//                self.boxNumberTextField.layer.borderColor = UIColor.hexStringToUIColor("#bbbbbb").CGColor
//                self.bagNumberTextField.layer.borderColor = UIColor.hexStringToUIColor("#bbbbbb").CGColor
            case .wrong:
                self.isUserInteractionEnabled = true
                self.boxNumberTextField.becomeFirstResponder()
//                self.boxNumberTextField.layer.borderColor = UIColor.redColor().CGColor
//                self.bagNumberTextField.layer.borderColor = UIColor.redColor().CGColor
            }
        }
    }
  


    lazy var boxNumberTextField: UITextField = {
        let textfield = self.makeTextField("", tag: 1001)
         textfield.tintColor = UIColor.hexStringToUIColor("#00acff")
         textfield.placeholder = "请输入"
        textfield.keyboardType = UIKeyboardType.numbersAndPunctuation
        return textfield
    }()
    lazy var bagNumberTextField: UITextField = {
        let textfield = self.makeTextField("", tag: 1002)
        textfield.tintColor = UIColor.hexStringToUIColor("#00acff")
        textfield.textAlignment = .center
        textfield.placeholder = "请输入"
        textfield.keyboardType = UIKeyboardType.numbersAndPunctuation
        return textfield
    }()
    
    lazy fileprivate var lineLabel: UILabel = {
        let label = UILabel()
        label.backgroundColor = UIColor.hexStringToUIColor("#cccccc")
        return label
    }()
    
    //MARK: - life cycle
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: UITableViewCellStyle.default, reuseIdentifier: "NumberInputCell")
        self.setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.setUpUI()
    }
    
    required init() {
        super.init(style: UITableViewCellStyle.default, reuseIdentifier: "NumberInputCell")
        self.setUpUI()
    }
    //MARK: - public
    override func clearContent() {
        self.bagNumberTextField.text = ""
        self.boxNumberTextField.text = ""
    }
    //MARK: - private
    func setUpUI(){
        self.boxNumberTextField.keyboardType = .numbersAndPunctuation
        self.bagNumberTextField.keyboardType = .numbersAndPunctuation
        self.state = inputViewState.disabled
        NotificationCenter.default.addObserver(self, selector: #selector(NumberInputCell.textfieldDidChanged(_:)), name: NSNotification.Name.UITextFieldTextDidChange, object: nil)
        self.iconView.image = UIImage(named: "Number")
        
        self.contentView.addSubview(self.lineLabel)
        
        self.contentView.addSubview(self.boxNumberTextField)
        self.contentView.addSubview(self.bagNumberTextField)
//        self.contentView.addSubview(self.singleNumberTextField)
        boxNumberTextField.snp.makeConstraints { (make) in
            make.leading.equalTo(self.contentView).offset(inputViewToCellSpace-10)
            make.top.equalTo(self.contentView).offset(FTCellTopSpace+5)
            make.bottom.equalTo(self.contentView).offset(-FTCellTopSpace-2)
            make.width.equalTo(90)
//              make.trailing.equalTo(self.contentView).offset(-10)
        }
        bagNumberTextField.snp.makeConstraints { (make) in
            make.leading.equalTo(lineLabel.snp.trailing).offset(FTCellHorizontalSpace)
            make.top.equalTo(self.contentView).offset(FTCellTopSpace+5)
            make.bottom.equalTo(self.contentView).offset(-FTCellTopSpace-2)
            make.trailing.equalTo(self.contentView).offset(-FTCellHorizontalSpace)
        }
        
        lineLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.contentView).offset(37)
            make.centerY.equalTo(self.contentView)
            make.width.equalTo(0.5)
            make.height.equalTo(28)
        }
        
    }
    
    fileprivate func makeTextField(_ placeHoder:String ,tag: NSInteger) -> (UITextField){
        let textfield = UITextField()
        textfield.tag = tag
//        textfield.backgroundColor = UIColor.yellowColor()
        textfield.delegate = self
//        textfield.layer.borderWidth = 1
//        textfield.layer.borderColor = UIColor.lightGrayColor().CGColor
        textfield.placeholder = placeHoder
        textfield.font = UIFont.systemFont(ofSize: self.getFont())
        textfield.textColor = UIColor.hexStringToUIColor("#666666")
        textfield.textAlignment = NSTextAlignment.left
        return textfield
    }
    
    //MARK: - delegate
    func getText() -> (String?) {
        return "\(boxNumberTextField.text ?? "")/\(bagNumberTextField.text ?? "")"
    }
    func SetText(_ text: String?) { // 使用text1/text2字符串格式传递左右两个输入框的文字
        let textArr = text?.components(separatedBy: "/")
        boxNumberTextField.text = textArr?.first
        bagNumberTextField.text = textArr?.count > 1 ? textArr?.last : ""
    }
    func isEmpty() -> (Bool) {
        return self.boxNumberTextField.text?.characters.count > 0 ? false : true
    }
    
    func finishInput(_ text: String?) {
        
    }
        
    //MARK: - UITextFieldDelegate
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        var futureString = textField.text ?? ""
        futureString.insert(contentsOf: string.characters, at: futureString.characters.index(futureString.startIndex, offsetBy: range.location))
        if textField == self.boxNumberTextField{
            return numFilter(precisionBox, string: futureString)
        }
        if textField == self.bagNumberTextField{
            return numFilter(precisionBag, string: futureString)
        }
        return true
    }
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        if textField.tag == 1001{ //第一个格子的回车默认跳转到第二个格子
            self.bagNumberTextField.becomeFirstResponder()
        }else{ //通知代理发起请求
            if self.delegate != nil {
                self.delegate?.finishNumInput(self)
            }
        }
        return true
    }
    func textfieldDidChanged(_ notification: Notification){

    }
    
}
